package com.leff.cards.ui.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.leff.cards.R;
import com.leff.cards.data.AppState;
import com.leff.cards.data.BaseGroup;
import com.leff.cards.data.Invitation;
import com.leff.cards.ui.util.GroupListAdapter;
import com.leff.cards.util.Secrets;
import com.leff.cards.web.QueryDispatcher;
import com.leff.cards.web.requests.InviteResponseRequest;
import com.leff.notifications.NotificationCenter;
import com.leff.notifications.NotificationListener;

public class InvitationsFragment extends SherlockFragment implements NotificationListener, Secrets, OnItemClickListener {

	private GroupListAdapter mAdapter;
	private TextView mNoInvitesText;
	
	private BaseGroup mActiveGroup;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mAdapter = new GroupListAdapter(getActivity(), AppState.get().getInvites());

		NotificationCenter.registerListener(this, AppState.INVITES_REFRESHED);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if(container == null) {
			return null;
		}
		View V = inflater.inflate(R.layout.groups_fragment, null);
		
		mNoInvitesText = (TextView)V.findViewById(R.id.no_groups_text);
		mNoInvitesText.setText(R.string.no_invites);
		ListView list = (ListView)V.findViewById(R.id.group_list);
		
		list.setOnItemClickListener(this);
		list.setAdapter(mAdapter);
		checkTextVisibility();
		
		return V;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		NotificationCenter.unregisterListener(this, AppState.INVITES_REFRESHED);
	}

	@Override
	public void onNotificationPosted(String action, Object data, Object sender) {
		mAdapter.setData(AppState.get().getInvites());
		checkTextVisibility();
	}

	@Override
	public void onItemClick(AdapterView<?> adapter, View V, int pos, long id) {
		
		mActiveGroup = (Invitation)mAdapter.getItem(pos);
		showAcceptDeclineDialog();
	}
	
	private void checkTextVisibility() {
		if(mAdapter.getCount() != 0) {
			mNoInvitesText.setVisibility(View.GONE);
		}
		else {
			mNoInvitesText.setVisibility(View.VISIBLE);
		}
	}
	
	private void sendInviteResponse(boolean join) {
		
		long id = mActiveGroup.id;
		long fbid = AppState.get().getUser().fbid;
		
		new QueryDispatcher(new InviteResponseRequest(id, fbid, join)).execute();
	}
	
	private void showAcceptDeclineDialog() {
		
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		
		builder.setTitle(R.string.accept_decline_title);
		String message = String.format(getActivity().getResources().getString(R.string.accept_decline_message), mActiveGroup.name);
		builder.setMessage(message);
		
		builder.setPositiveButton(R.string.join, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				sendInviteResponse(true);
			}
		});
		builder.setNegativeButton(R.string.decline, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				sendInviteResponse(false);
			}
		});
		
		builder.create().show();
	}
}
